// void, Obj me, Obj master_obj

Unit this, u, master;
point offs;
bool b;
int l;

Sleep(rand(100)+30);
this = me.AsUnit();
master = master_obj.AsUnit();
offs = .posRH - master.posRH;

while (master.IsAlive()) 
{
  u = .BestTargetInSquadSight();
  
  if(u.IsValid()){
		
		if(master.AsHero.army.count < master.AsHero.maxarmy)
			.AddCommand(true, "attach", master);
		else
			.AddCommand(true, "protect", master_obj);
		
		Sleep(rand(200)+100);
		.AddCommand(true, "engage");
		return;
		
	} else {
		if (.Goto(master.dest + offs, .range, 2000, true, -1)){
			.Idle(2000);
		}
	}

	if(master.IsHeirOf("Hero")){
		if(master.AsHero.army.count < master.AsHero.maxarmy){
			Sleep(rand(200)+100);
			.AddCommand(true, "attach", master);
			return;
		}
	}
}

//.pr("unit::protect: protected object is dead");
